﻿using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.Domain.Interface;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntelligentAgriculture.Domain.Impl
{
    /// <summary>
    /// 羊只转舍仓储实现
    /// </summary>
    public class SheepRescissionRepository : ISheepRescissionRepository
    {
        /// <summary>
        /// 数据上下文
        /// </summary>
        private readonly AgricultureDbContext _context;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="context"> 数据上下文 </param>
        public SheepRescissionRepository(AgricultureDbContext context)
        {
            _context = context;
        }
        /// <summary>
        /// 获取羊只转舍列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<Sheeprescission>> GetSheepRescission()
        {
            var list = await _context.Sheeprescissions.Where(p => p.Isdel == false).ToListAsync();
            return list;
        }
        /// <summary>
        /// 新增羊只转舍
        /// </summary>
        /// <param name="sheeprescission">羊只转舍实体</param>
        /// <returns></returns>
        public async Task<int> AddSheepRescission(List<Sheeprescission> sheeprescission)
        {
            await _context.Sheeprescissions.AddRangeAsync(sheeprescission);
            return await _context.SaveChangesAsync();
        }
        /// <summary>
        /// 删除羊只转舍
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<int> DelSheepRescission(int userId, params int[] ids)
        {
            if (ids.Length <= 0)
            {
                //没有要删除的数据
                return 0;
            }
            var sheeprescissions = await _context.Sheeprescissions.Where(x => ids.Contains(x.SheepRescissionId)).ToListAsync();
            foreach (var sheeprescission in sheeprescissions)
            {
                sheeprescission.Isdel = true;
                sheeprescission.UpdateTime = DateTime.Now;
                sheeprescission.UpdateName = _context.Userinfos.FirstOrDefault(p => p.Uid == userId).Uname;
            }
            return await _context.SaveChangesAsync();
        }
    }
}
